package com.yix.admin.modules.system.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.yix.common.base.BaseUUIDEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Objects;
import java.util.Set;

/**
 * @author wdx
 * @version 1.0
 * @Description TODO
 * @date 2021/1/27 17:26
 */
@Entity
@Data
@Table(name="sys_dept")
@org.hibernate.annotations.Table(appliesTo = "sys_dept",comment = "部门表")
public class SysDept extends BaseUUIDEntity implements Serializable {

    @JSONField(serialize = false)
    @ManyToMany(mappedBy = "depts")
    @ApiModelProperty(value = "角色")
    private Set<SysRole> roles;

    @Column(columnDefinition = "int(20) COMMENT '排序'",nullable = true)
    @ApiModelProperty(value = "排序")
    private Integer sort;

    @NotBlank
    @Column(columnDefinition = "varchar(255) COMMENT '名称'")
    @ApiModelProperty(value = "名称")
    private String name;

    @Column(columnDefinition = "bit(1) COMMENT '状态'")
    @ApiModelProperty(value = "状态")
    private Boolean enabled;

    @Column(columnDefinition = "varchar(32) COMMENT '上级部门'")
    @ApiModelProperty(value = "上级部门")
    private String pid;

    @ApiModelProperty(value = "子节点数目", hidden = true)
    private Integer subCount = 0;

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        SysDept dept = (SysDept) o;
        return Objects.equals(id, dept.id) &&
                Objects.equals(name, dept.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name);
    }
}
